package com.renwei.yunlong.activity.me;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.AddressJsonBean;
import com.renwei.yunlong.event.AddressLoadEvent;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.service.AddressLoadService;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.service
 * Created by :  Benjamin.
 * Created time: 2017/10/19 14:56
 * Changed by :  Benjamin.
 * Changed time: 2017/10/19 14:56
 * Class description:
 */

public class RegisterAddressActivity extends BaseActivity implements View.OnClickListener, OptionsPickerView.OnOptionsSelectListener {


    @Bind(R.id.rl_select_address)
    RelativeLayout rlSelectAddress;//选择省市区
    @Bind(R.id.tv_tt_address)
    TextView tvAddressTitle;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.iv_next)
    ImageView ivNext;
    @Bind(R.id.edit_address)
    EditText editAddress;
    Intent intent;
    OptionsPickerView pvOptions;
    boolean isLoadAddressSuccess = false;
    private String provinceId = "";
    private String cityId = "";
    private String regionId = "";
    private String detailAddress ="";
    public static ArrayList<AddressJsonBean> options1Items = new ArrayList<>();
    public static ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    public static ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_address);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        provinceId = getIntent().getStringExtra("provinceId") == null ? "" : getIntent().getStringExtra("provinceId");
        cityId = getIntent().getStringExtra("cityId") == null ? "" : getIntent().getStringExtra("cityId");
        regionId = getIntent().getStringExtra("regionId") == null ? "" : getIntent().getStringExtra("regionId");
        detailAddress = getIntent().getStringExtra("detailAddress") == null ? "" : getIntent().getStringExtra("detailAddress");
        initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                checkSelect();
                break;
            case R.id.rl_select_address:
                pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
                pvOptions.show();
                break;
            default:
                break;

        }
    }


    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.VISIBLE);
        tvTitle.setText("注册地址");
        tvRight.setText("保存");
        ivBack.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        rlSelectAddress.setOnClickListener(this);
        tvAddressTitle.setText(StringUtils.value(provinceId + cityId + regionId));
        editAddress.setText(StringUtils.value(detailAddress));
        editAddress.setFilters(new InputFilter[]{AppHelper.emojiFilter});
        editAddress.setSelection(StringUtils.value(detailAddress).length());
        startService(new Intent(this, AddressLoadService.class));
        intent = new Intent();
        //选项选择器
        pvOptions = new OptionsPickerView.Builder(this, this).setTitleText("选择地址")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(getResources().getColor(R.color.text_dark_black_color)) //设置选中项文字颜色
                .setTextColorOut(getResources().getColor(R.color.color_666666))
                .setDividerColor(getResources().getColor(R.color.color_999999))
                .setCancelText("取消")
                .setCancelColor(getResources().getColor(R.color.color_999999))
                .setSubmitText("确定")
                .setSubmitColor(getResources().getColor(R.color.blue_color))
                .setContentTextSize(20)
                .setTitleSize(17)
                .build();
    }


    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(AddressLoadEvent event) {
        switch (event.getLoadResult()) {
            case AddressLoadEvent.loadSuccess:
                //加载成功
                isLoadAddressSuccess = true;
                break;
            case AddressLoadEvent.loadError:
                //加载失败
                isLoadAddressSuccess = false;
                showCenterInfoMsg("地址信息拉取失败");
                break;
                default:
                break;
        }
    }


    private void checkSelect() {
        if (tvAddressTitle.getText() != null && tvAddressTitle.getText().length() > 0) {
            if (editAddress.getText() != null && editAddress.getText().toString().length() > 0) {
                intent.putExtra("provinceId", provinceId);
                intent.putExtra("cityId", cityId);
                intent.putExtra("regionId", regionId);
                intent.putExtra("registerAddr", editAddress.getText().toString());
                setResult(999, intent);
                this.finish();
            } else {
                showCenterInfoMsg("请填写详细地址");
            }
        } else {
            showCenterInfoMsg("请先选择地址");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onOptionsSelect(int options1, int options2, int options3, View v) {
        provinceId = StringUtils.value(options1Items.get(options1).getName());
        cityId = StringUtils.value(options2Items.get(options1).get(options2));
        regionId = StringUtils.value(options3Items.get(options1).get(options2).get(options3));
        String tx = StringUtils.value(provinceId + cityId + regionId);
        tvAddressTitle.setText(tx);
        tvAddressTitle.setTextColor(getResources().getColor(R.color.color_333333));
    }
}
